unit uTransBilling;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, AdvSmoothPanel, AdvSmoothTabPager, AdvGlowButton, DynamicSkinForm,
  StdCtrls, AdvCombo, AdvEdit, AdvEdBtn;

type
  TFrmTransBilling = class(TForm)
    PanelTop: TAdvSmoothPanel;
    AdvGlowButton3: TAdvGlowButton;
    spDynamicSkinForm1: TspDynamicSkinForm;
    BtnSave: TAdvGlowButton;
    Durasi: TAdvEdit;
    Guest: TAdvEdit;
    Label1: TLabel;
    MemberID: TAdvEditBtn;
    PaketPromo: TAdvEditBtn;
    Waiter: TAdvEditBtn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure AdvGlowButton3Click(Sender: TObject);
    procedure BtnSaveClick(Sender: TObject);
    procedure MemberIDKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure PaketPromoClickBtn(Sender: TObject);
    procedure MemberIDClickBtn(Sender: TObject);
    procedure PaketPromoKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure WaiterClickBtn(Sender: TObject);
    procedure WaiterKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private declarations }
    procedure initForm;
    function isValid: boolean;
    function isSaved: Boolean;
    function getDurasi: TDateTime;
    function getNum: String;
    function getHargaSewa(ID: integer; ADurasi: double): Double;
    function getDisc(MemID: integer): double;
  public
    { Public declarations }
  end;

var
  FrmTransBilling: TFrmTransBilling;

implementation

uses uBilling, uDM, uLookup;

{$R *.dfm}

procedure TFrmTransBilling.MemberIDClickBtn(Sender: TObject);
begin
  OpenSQL(DM.Lookup,'select m.MemberId as ID, m.Nama, m.Kota, m.Telepon, k.Disc, k.DiscFB '+
  'from member m join member_kategori k on k.Id=m.IdKategori where m.Expired > now() order by m.nama ');
  Dm.Lookup.ReadOnly := True;
  bukaForm(FrmLookup, TFrmLookup, true, false);
  MemberID.Text := DM.Lookup.Fields[1].AsString;
  MemberID.Tag  := DM.Lookup.Fields[0].AsInteger;
  Guest.Text    := DM.Lookup.Fields[1].AsString;
  DM.Disc       := DM.Lookup.Fields[4].AsFloat;
  DM.DiscFb     := DM.Lookup.Fields[5].AsFloat;
end;

procedure TFrmTransBilling.BtnSaveClick(Sender: TObject);
begin
  if isValid then
    isSaved;
end;

procedure TFrmTransBilling.AdvGlowButton3Click(Sender: TObject);
begin
  close;
end;

procedure TFrmTransBilling.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree;
end;

procedure TFrmTransBilling.FormCreate(Sender: TObject);
begin
//  MakeTransparent;
end;

procedure TFrmTransBilling.FormShow(Sender: TObject);
begin
  PanelTop.Caption.Text := ' :: '+DM.RoomName+' ::';
  FrmBilling.Timer2.Enabled := True;
  initForm;
end;

function TFrmTransBilling.getDisc(MemID: integer): double;
begin
//  OpenSQL(DM.Buffer, 'select ');
end;

function TFrmTransBilling.getDurasi: TDateTime;
var ADurasi: String;
begin
  ADurasi := FloatToStr(Durasi.FloatValue * 60);
  OpenSQL(dm.Buffer,'select date_add(NOW(), interval '+ADurasi+' minute)');
  Result := dm.Buffer.Fields[0].AsDateTime;
end;


function TFrmTransBilling.getHargaSewa(ID: integer; ADurasi: double): Double;
begin
  Result := 0;
  if ID> 0 then
    Result := getIntFromSQL(DM.Buffer, 'select IF('+FloatToStr(ADurasi)+'>=MinDurasi, Rate, RateNormal) '+
    'from room_harga_sewa where id='+IntToStr(ID));
end;

function TFrmTransBilling.getNum: String;
var
  Depan,Belakang : String;
  IBelakang : Integer;
Begin
Depan := FormatDateTime('yymm',Date);
OpenSQL(DM.Buffer,'select max(NoTransaksi) from transaksi_header '+
  'where SUBSTRING(NoTransaksi,1,4)='+QuotedStr(Depan));
with DM.Buffer Do
Begin
   if (RecordCount = 0) or (Length(Fields[0].AsString)=0) then
      Belakang:= '00001'
   else
      Begin
          Belakang:=Copy(Fields[0].AsString,5,5);
          IBelakang := StrToInt(Belakang);
          Inc(IBelakang);

          if IBelakang < 10 then
          Belakang:='0000'+IntToStr(IBelakang)
          else
          if (IBelakang>=10) and (IBelakang<100) then
          Belakang:='000'+IntToStr(IBelakang)
          else
          if (IBelakang>=100) and (IBelakang<1000) then
          Belakang:='00'+IntToStr(IBelakang)
          else
          if (IBelakang>=1000) and (IBelakang<10000) then
          Belakang:='0'+IntToStr(IBelakang)
          else
          if (IBelakang>=10000) and (IBelakang<100000) then
          Belakang:=IntToStr(IBelakang)
          else
          Begin
            MessageDlg('No Transaksi Overload.....',mtError,[mbOk],0);
            Application.Terminate;
          end;
      End;
   Result :=Depan+Belakang;
end;
end;

procedure TFrmTransBilling.initForm;
begin
  PaketPromo.Clear;
  PaketPromo.Tag := 0;
  MemberID.Clear;
  MemberID.Tag := 0;
  Guest.Clear;
  Durasi.FloatValue := 1;
  Waiter.Clear;
  Waiter.Tag := 0;
//  PaketPromo.SetFocus;
end;

function TFrmTransBilling.isSaved: Boolean;
var  noTrans: String;
     ATgl, ADurasi : TDateTime;
begin
  Result := True;
  DM.Connection.StartTransaction;
  try
    noTrans := getNum;

    ATgl:= serverNow;
    ADurasi := getDurasi;

    ExecSQL(DM.Buffer, 'insert into transaksi_header '+
    '(NoTransaksi,Tanggal,NamaPelanggan,IdMember,Diskon,TypeDisc,Nama_Karyawan,Mulai,Selesai,PPN)'+
    'Values ('+QuotedStr(noTrans)+','+ formatSQLDate(ATgl,0)+','+ QuotedStr(Guest.Text)+','+
    IntToStr(MemberID.Tag)+','+FloatToStr(DM.Disc)+','+ FloatToStr(DM.DiscFb)+','+ QuotedStr(Waiter.Text)+','+
    formatSQLDate(ATgl,0)+','+formatSQLDate(ADurasi,0)+',1)');

    ExecSQL(DM.Buffer, 'insert into transaksi_room '+
    '(NoTransaksi, IdRoom, Mulai, Selesai, IdHargaSewa, Jml_JamPerson, HargaPerJam, Harga) '+
    'values ('+QuotedStr(noTrans)+','+QuotedStr(DM.RoomID)+','+
    formatSQLDate(ATgl,0)+','+formatSQLDate(ADurasi,0)+','+
    IntToStr(PaketPromo.Tag)+','+FloatToStr(Durasi.FloatValue)+','+
    FloatToStr(getHargaSewa(PaketPromo.Tag, Durasi.FloatValue))+','+
    FloatToStr(getHargaSewa(PaketPromo.Tag, Durasi.FloatValue)* Durasi.FloatValue)+')');

    ExecSQL(DM.Buffer, 'update room set '+
    'NoTransaksi = '+QuotedStr(noTrans)+','+
    'Mulai = '+formatSQLDate(ATgl, 0)+','+
    'Selesai = '+formatSQLDate(ADurasi, 0)+','+
    'Nama = '+QuotedStr(Guest.Text)+' where id_room=' +QuotedStr(DM.RoomID));

    DM.Connection.Commit;
    Inform(MSG_SAVE_SUCCES);
    Close;
  except
    DM.Connection.Rollback;
    Inform(MSG_SAVE_UNSUCCES);
    Result := false;
  end;
end;

function TFrmTransBilling.isValid: boolean;
begin
  Result:= False;
  if CekInput(Result, PaketPromo.Text <> '', 'Paket Promo belum diisi.', PaketPromo) then
  if CekInput(Result, Guest.Text <> '','Guest belum diisi.', MemberID) then
  if CekInput(Result, Durasi.FloatValue > 0, 'Durasi belum sesuai.', Durasi) then
      CekInput(Result, Waiter.Text <> '', 'Waiter belum sesuai.', Waiter);
end;

procedure TFrmTransBilling.MemberIDKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if key=vk_f2 then
  MemberIDClickBtn(Sender);
if key=vk_delete then
  MemberID.Tag := 0;
end;

procedure TFrmTransBilling.PaketPromoClickBtn(Sender: TObject);
var IDKat : Integer;
begin
//  Inform(IntToStr(DayOfWeek(serverNow)));
  IDKat := getIntFromSQL(DM.Buffer, 'select id_kategory from room where id_room = '''+DM.RoomID+'''');

  OpenSQL(DM.Lookup, 'select s.ID, p.RoomPaket as Paket from room_harga_sewa s join room_paket p on s.IdRoomPaket=p.Id '+
  'where IdRoomKategory = '+IntToStr(IDKat)+
//  ' and '+QuotedStr(formatSQLDate(serverNow,3))+' BETWEEN '+
//  ' s.JamAwal and s.JamAkhir ');
  ' and '+IntToStr(DayOfWeek(serverNow))+
  ' in (select DayOf from room_harga_sewa_hari where IdHargaRomSewa=s.id) ');

  Dm.Lookup.ReadOnly := True;
  bukaForm(FrmLookup, TFrmLookup, True, False);
  PaketPromo.Text := DM.Lookup.Fields[1].AsString;
  PaketPromo.Tag := DM.Lookup.Fields[0].AsInteger;
end;

procedure TFrmTransBilling.PaketPromoKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key=vk_f2 then
    PaketPromoClickBtn(Sender);
end;

procedure TFrmTransBilling.WaiterClickBtn(Sender: TObject);
begin
  OpenSQL(DM.Lookup,'select UserID as ID, Nama from user order by nama ');
  bukaForm(FrmLookup, TFrmLookup, true, false);
  Waiter.Text := DM.Lookup.Fields[1].AsString;
  Waiter.Hint  := DM.Lookup.Fields[0].AsString;
end;

procedure TFrmTransBilling.WaiterKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key=vk_f2 then
    WaiterClickBtn(Sender);
end;

end.
